﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
//using BSCMFF.Core.DomainService;
//using BSCMFF.Core.DataAccessLayer;
using System.ServiceModel.DomainServices.Client;
using BSCMFF.Web.DomainService;
//using BSCMFF.Core.DataAccessLayer;
using Microsoft.Expression.Interactivity.Layout;
using System.Windows.Interactivity;
using StrategyMap.UserControls;
using StrategyMap.BusinessLayer;
using Microsoft.Expression.Controls;
using Microsoft.Expression.Media;

namespace StrategyMap
{
    public partial class MainPage : UserControl
    {
        private Node1 selectedNode;
        private LineArrow selectedArrow;

        public MainPage()
        {
            InitializeComponent();

            //Node1 n = new Node1();
            //n.StrategyName.Text = "load";
            //n.Name = "idload";

            //Canvas.SetLeft(n, 150);
            //Canvas.SetTop(n, 100);
            //MouseDragElementBehavior drag = new MouseDragElementBehavior();
            //drag.ConstrainToParentBounds = true;
            ////Interaction.GetBehaviors(n).Add(drag);
            //n.StartDrawingArrowEvent += new MouseButtonEventHandler(Node_StartDrawingArrowEvent);
            //LayoutRoot.Children.Add(n);
            //nodes1.Add(n);
   //         Map1.InitializeComponent();
            //Map1.MouseLeftButtonDown += new MouseButtonEventHandler(Map1_MouseLeftButtonDown);
            Map1.EventLineArrowLeftButtonDown += new EventHandler(Map1_EventLineArrowLeftButtonDown);

            Map1.EventLineArrowCreated += new EventHandler(Map1_EventLineArrowCreated);

            Map1.EventNodeClicked += new EventHandler(Map1_EventNodeClicked);


            Map1.MouseLeftButtonDown += new MouseButtonEventHandler(Map1_MouseLeftButtonDown);
           
            //Map1.Unloaded += new RoutedEventHandler(Map1_Unloaded);
        }

        void Map1_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            string RecName = ((FrameworkElement)e.OriginalSource).Name;

            if( RecName == "F" || RecName == "C" || RecName ==  "I" || RecName ==  "L")
            {
                CancelCurrentArrowSelected();
                CancelCurrentNodeSelected();
                
            }
        }

        //void Map1_Unloaded(object sender, RoutedEventArgs e)
        //{
        //    string s = "";
        //}

        //private void Application_Exit(object sender, EventArgs e)
        //{
        //    string s = "";
        //}

        void Map1_EventNodeClicked(object sender, EventArgs e)
        {
            //if(selectedArrow != null)
            CancelCurrentArrowSelected();

            if (selectedNode != null && selectedNode == (Node1)sender)
            {
                return;
            }
            CancelCurrentNodeSelected();
            selectedNode = ((Node1)sender);
            //selectedNode.RemoveMouseLeave();
            //selectedNode.HideRectagles();
            selectedNode.ChangeMouseLeave_Click();


            //Node1 n = ((Node1)sender);
            //Test.Text = "node clicked" + selectedNode.DataContext;

            SetNodeDetails();
        }

        private void SetNodeDetails()
        {
            ID.Text = selectedNode.Name;
            Type.Text = "node";

            string[] parsed = selectedNode.DataContext.ToString().Split('|');

            CurrentName.Text = parsed[0]; //selectedArrow.DataContext.ToString();
            if (parsed.Length > 1)
            {
                CurrentDescription.Text = parsed[1];
            }
            //sliderBend.Visibility = Visibility.Collapsed;

            sliderBend.ValueChanged -= sliderBend_ValueChanged;
            sliderBend.Minimum = 0;
            sliderBend.Maximum = 50;
            sliderBend.Value = selectedNode.Radius;// 25; //la.blendAmount
            sliderBend.ValueChanged += sliderBend_ValueChanged;

            if (selectedNode.Main.Fill == null)
            {
                CP.Color = Colors.Transparent;
            }
            else
            {
                CP.Color = ((SolidColorBrush)selectedNode.Main.Fill).Color;
            }

            if (selectedNode.StrategyName.Foreground == null)
            {
                CPText.Color = Colors.Black;
            }
            else
            {
                CPText.Color = ((SolidColorBrush)selectedNode.StrategyName.Foreground).Color;
            }
            CPText.Visibility = Visibility.Visible;
        }

        private void CancelCurrentNodeSelected()
        {
            if (selectedNode != null)
            {
                selectedNode.ChangeMouseLeave_UnClick();
                selectedNode = null;
            }
        }

        void Map1_EventLineArrowCreated(object sender, EventArgs e)
        {
            CancelCurrentNodeSelected();
            if (selectedArrow != null)
            {
                selectedArrow.StrokeThickness = 3;
                Map1.AddMouseLeave(selectedArrow);
            }
            selectedArrow = ((LineArrow)sender);
            //Test.Text = "arrow created ";

            SetArrowDetails();
        }

        void Map1_EventLineArrowLeftButtonDown(object sender, EventArgs e)
        {
            CancelCurrentNodeSelected();

            if (selectedArrow != null && selectedArrow == (LineArrow)sender)
            {
                return;
            }
            CancelCurrentArrowSelected();
            
            selectedArrow = ((LineArrow)sender);
            //selectedNode.ChangeMouseLeave_Click();

            //Test.Text = "arrow clicked " + ((LineArrow)sender).DataContext.ToString() ;

            SetArrowDetails();

        }

        private void CancelCurrentArrowSelected()
        {
            if (selectedArrow != null)
            {
                selectedArrow.StrokeThickness = 3;
                Map1.AddMouseLeave(selectedArrow);
                selectedArrow = null;
            }
        }

        private void SetArrowDetails()
        {
            CPText.Visibility = Visibility.Collapsed;
            if (selectedArrow != null)
            {
                ButtonDelete.Visibility = System.Windows.Visibility.Visible;
                //LineArrow la = ((LineArrow)sender);

                ID.Text = selectedArrow.Name;
                Type.Text = "arrow";

                string[] parsed = selectedArrow.DataContext.ToString().Split('|');

                CurrentName.Text = parsed[0]; //selectedArrow.DataContext.ToString();
                if (parsed.Length > 1)
                {
                    CurrentDescription.Text = parsed[1];
                }
                //sliderBend.Visibility = Visibility.Visible;
                sliderBend.ValueChanged -= sliderBend_ValueChanged;

                sliderBend.Minimum = -1;
                sliderBend.Maximum = 1;
                sliderBend.Value = selectedArrow.BendAmount;// 0; //la.blendAmount
                sliderBend.ValueChanged += sliderBend_ValueChanged;

                CP.Color = ((SolidColorBrush)selectedArrow.Stroke).Color;
            }
            else
            {
                ButtonDelete.Visibility = System.Windows.Visibility.Collapsed;
                ID.Text = "";
                Type.Text = "";
                CurrentName.Text = "";
                CurrentDescription.Text = "";
                sliderBend.Minimum = -1;
                sliderBend.Maximum = 1;
                sliderBend.Value = 0;
                CP.Color = Colors.Transparent;
            }
        }

        

        //private void ButtonSaveDetails_Click(object sender, System.Windows.RoutedEventArgs e)
        //{
            
        //    switch (Type.Text)
        //    {
        //        case "node": ChangeNode(sender); break;
        //        case "arrow": ChangeArrow(sender); break;
        //        default: Test.Text = "button click"; break;
        //    }}


        private void sliderBend_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
        {
            if (selectedArrow != null)
            {
                selectedArrow.BendAmount = sliderBend.Value;             
            }

            if (selectedNode != null)
            {
                selectedNode.Radius = (int)sliderBend.Value;                
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            Map1.LoadNodesAndArrows();
        }

        private void CP_ColorChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (selectedArrow != null)
            {
                selectedArrow.Stroke = new SolidColorBrush(CP.Color);            
            }

            if (selectedNode != null)
            {
                selectedNode.Main.Fill = new SolidColorBrush(CP.Color);             
            }
                   
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            Map1.SaveStrategyMap();
        }

        private void ButtonDelete_Click(object sender, RoutedEventArgs e)
        {
            if (selectedArrow != null)
            {
                Map1.DeleteSelectedArrow(selectedArrow);

                
            }
        }

        private void ButtonImage_Click(object sender, RoutedEventArgs e)
        {
            Map1.CreateFile();
        }

        private void ButtonPrint_Click(object sender, RoutedEventArgs e)
        {
            Map1.Print_Map();
        }

        private void CurrentName_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (selectedArrow != null)
            {
                //string[] parsed = selectedArrow.DataContext.ToString().Split('|');

                selectedArrow.DataContext = CurrentName.Text + "|" + CurrentDescription.Text;
            }

            if (selectedNode != null)
            {
                //string[] parsed = selectedNode.DataContext.ToString().Split('|');

                //selectedNode.DataContext = CurrentName.Text + "|" + parsed[1];
            }
        }

        private void CurrentDescription_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (selectedArrow != null)
            {
                //string[] parsed = selectedArrow.DataContext.ToString().Split('|');

                selectedArrow.DataContext = CurrentName.Text + "|" + CurrentDescription.Text;
            }

        }

        private void CPText_ColorChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            if (selectedNode != null)
            {
                selectedNode.StrategyName.Foreground = new SolidColorBrush(CPText.Color);
            }
        }

        
    }
}
